import { ShowAD } from '../../CocosAd/ShowAD';
import QuickTool from '../base/Tool/QuickTool';
const {ccclass, property} = cc._decorator;

@ccclass
export default class TopMenuView extends cc.Component {

    @property(cc.Label)
    scoreLabel: cc.Label = null;

    @property(cc.Label)
    targetScoreLabel: cc.Label = null;

    @property(cc.Label)
    errorCountLabel: cc.Label = null;

    @property(cc.Label)
    titleLabel: cc.Label = null;

    @property(cc.Label)
    rightLabel: cc.Label = null;

    @property([cc.Node])
    bottomNodes: cc.Node[] = new Array<cc.Node>();

    pauseCall: ()=>void = null;

    time = 0;
    useTime = 0;
    canTime = false;
    timeEnd : any;
    rookie = false;

    onLoad () {
        this.node.getChildByName("Titles").getChildByName("New Button").on('click', ()=>{
            QuickTool.safeCall1(this.pauseCall)
        })
        this.rightLabel.string = ""
    }

    startRightTime (t = null, end: any= null) {
        if (this.rookie) {return}
        
        if (t != null) {
            this.time = t
            this.timeEnd = end
            this.useTime = 0
            this.rightLabel.string = Math.ceil(this.time) + "S"
        }
        this.canTime = true
    }

    pauseRightTime () {
        
        this.canTime = false
    }

    onDestroy () {
        if (this.timeEnd != null) {
            this.timeEnd = null
        }
    }

    update (dt: number) {

        if (this.canTime) {
            this.time -= dt
            this.useTime += dt
            this.rightLabel.string = Math.ceil(this.time) + "S"
            if (this.time <= 0) {
                if (this.timeEnd != null) {
                    this.timeEnd()
                    this.timeEnd = null
                }
                this.canTime = false
            }
        }
    }

    hideBottom (): TopMenuView {
        this.bottomNodes.forEach(e=>e.active=false)
        return this
    }
    
    scoreCenterAndShow (): TopMenuView {
        this.bottomNodes[0].active = true
        this.bottomNodes[0].setPosition(this.bottomNodes[1].getPosition())
        return this
    }

    GetTableVideo() {
        // 生成一个随机整数，范围在1到6之间
        let randomInt = Math.floor(Math.random() * 6) + 1;
        let TableScreenADStr = "ml0d60l7lb925l122b";

        // 根据随机数选择对应的表格屏幕广告字符串
        switch (randomInt) {
            case 1:
                TableScreenADStr = "ml0d60l7lb925l122b";
                break;
            case 2:
                TableScreenADStr = "5a8l889g7fi38126b1";
                break;
            case 3:
                TableScreenADStr = "25qadh17ftlha364eb";
                break;
            case 4:
                TableScreenADStr = "76f540i9k2ceambo8d";
                break;
            case 5:
                TableScreenADStr = "cgiml81jel6812308d";
                break;
            case 6:
                TableScreenADStr = "1h39988df519fjedgh";
                break;
            default:
                TableScreenADStr = "1h39988df519fjedgh";
                break;
        }

        // 返回选中的表格屏幕广告字符串
        return TableScreenADStr;
    }

    public showRewardedAd() {
            cc.director.pause();
            // 请求激励广告
            let videoAd = tt.createRewardedVideoAd({
                //adUnitId: "k8b67ifdfg3k578j8a",
                adUnitId: this.GetTableVideo(),
                multiton: true,
                multitonRewardMsg: ['更多奖励1', '更多奖励2', '更多奖励3'],
                multitonRewardTimes: 999,
                progressTip: false,
            });
            videoAd.show().then(() => {
                console.log("视频广告展示");
                cc.director.resume();
            });
            videoAd.onClose(res => {
                // 用户点击了【关闭广告】按钮
                // 小于 2.1.0 的基础库版本，res 是一个 undefined
                if (res && res.isEnded || res === undefined) {
                    // 正常播放结束，可以下发游戏奖励的逻辑
                    this.addTime();
                    window.maidianback(function(res) {
                        console.log("回调结果: ", res);
                    });
                    console.log(`观看成功`);
                    console.log(`watch ${res.count} video`);
                    cc.director.resume();
                    //todo：插屏广告等待播放的计时恢复...             
                }
                else {
                    // 播放中途退出，不下发游戏奖励
                    cc.director.resume();
                    //todo：插屏广告等待播放的计时恢复...    
                }
            });
        }
    

    addTime () {   
            this.time += 30
            this.rightLabel.string = Math.ceil(this.time) + "S" // 更新显示
    }
}

/**
 * public void NOSomethingA()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingB()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingC()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingD()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingE()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingF()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingG()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingH()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingI()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingJ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingK()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingL()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingM()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingN()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingO()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingP()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingQ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingR()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingS()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingT()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingU()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingV()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
 */
